package com.movecar.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.style.AbsoluteSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.movecar.R;
import com.movecar.model.GroupBean;
import com.movecar.ui.GroupDetailActivity;
import com.movecar.utils.AppConfig;
import com.movecar.utils.imageloader.Constants;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GroupListAdapter extends BaseAdapter {

	private Activity activity;
	private LayoutInflater inflater;
	private ViewHolder viewHolder = null;
	private List<GroupBean> resultList = new ArrayList<GroupBean>();
	private ImageLoader imageLoader = null;

	TextView group_title, group_time, group_address, join_num, discuss_num, joined_num, limit_text, group_content,
			feedbackCoin;
	LinearLayout myLayout;
	ImageView group_img, isjoin, activity_img, group_time_img;
	Button group_join, group_discuss;

	public GroupListAdapter(Activity activity, List<GroupBean> resultList) {
		this.resultList = resultList;
		this.activity = activity;
		inflater = LayoutInflater.from(activity);
		imageLoader = ImageLoader.getInstance();

	}

	@Override
	public int getCount() {

		return resultList.size();
	}

	@Override
	public Object getItem(int arg0) {

		return resultList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {

		return arg0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {
		final ViewHolder viewHolder;
		if (view == null) {
			viewHolder = new ViewHolder();
			view = inflater.inflate(R.layout.club_detail_item, null);
			viewHolder.group_img = (ImageView) view.findViewById(R.id.group_img);
			viewHolder.group_title = (TextView) view.findViewById(R.id.group_title);
			viewHolder.joined_num = (TextView) view.findViewById(R.id.group_joined_num);
			viewHolder.limit_num = (TextView) view.findViewById(R.id.group_limit_num);
			viewHolder.group_time = (TextView) view.findViewById(R.id.group_time);
			viewHolder.feedbackCoin = (TextView) view.findViewById(R.id.feedbackCoin);
			viewHolder.group_description = (TextView) view.findViewById(R.id.group_description);
			viewHolder.berserk = (ImageView) view.findViewById(R.id.berserk);
			viewHolder.invitation = (ImageView) view.findViewById(R.id.invitation);
			viewHolder.group_type = (ImageView) view.findViewById(R.id.group_type);
			viewHolder.rmb = (TextView) view.findViewById(R.id.rmb);
			viewHolder.make_an_appointment = (TextView) view.findViewById(R.id.make_an_appointment);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}

		// 判断添加标签
		if ("1".equals(resultList.get(position).getIsBerserk())) {
			viewHolder.berserk.setVisibility(View.VISIBLE);
		} else {
			viewHolder.berserk.setVisibility(View.GONE);
		}
		// 抢机会
		if ("0".equals(resultList.get(position).getJoinFlag())) {
			viewHolder.limit_num.setVisibility(View.VISIBLE);
			viewHolder.group_type.setVisibility(View.VISIBLE);
			viewHolder.joined_num.setVisibility(View.VISIBLE);
			viewHolder.make_an_appointment.setVisibility(View.GONE);
			viewHolder.group_type.setImageResource(R.drawable.group_recruit);
			viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num1);
			viewHolder.joined_num.setTextColor(activity.getResources().getColor(R.color.main_title));
			viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
			// 已抢到
		} else if ("1".equals(resultList.get(position).getJoinFlag())) {
			viewHolder.limit_num.setVisibility(View.VISIBLE);
			viewHolder.group_type.setVisibility(View.VISIBLE);
			viewHolder.joined_num.setVisibility(View.VISIBLE);
			viewHolder.make_an_appointment.setVisibility(View.GONE);
			viewHolder.group_type.setImageResource(R.drawable.group_joined);
			viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num2);
			viewHolder.joined_num.setTextColor(activity.getResources().getColor(R.color.group_make_an_appointment));
			viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
			// 已参加
		} else if ("2".equals(resultList.get(position).getJoinFlag())) {
			viewHolder.limit_num.setVisibility(View.VISIBLE);
			viewHolder.group_type.setVisibility(View.VISIBLE);
			viewHolder.joined_num.setVisibility(View.VISIBLE);
			viewHolder.make_an_appointment.setVisibility(View.GONE);
			viewHolder.group_type.setImageResource(R.drawable.group_joined);
			viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num2);
			viewHolder.joined_num.setTextColor(activity.getResources().getColor(R.color.group_make_an_appointment));
			viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
			// 已过期
		} else if ("3".equals(resultList.get(position).getJoinFlag())) {
			viewHolder.limit_num.setVisibility(View.VISIBLE);
			viewHolder.group_type.setVisibility(View.VISIBLE);
			viewHolder.joined_num.setVisibility(View.VISIBLE);
			viewHolder.make_an_appointment.setVisibility(View.GONE);
			viewHolder.group_type.setImageResource(R.drawable.icon_over);
			viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num4);
			viewHolder.joined_num.setTextColor(activity.getResources().getColor(R.color.group_over));
			viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
			// 报名止
		} else if ("4".equals(resultList.get(position).getJoinFlag())) {
			viewHolder.limit_num.setVisibility(View.VISIBLE);
			viewHolder.group_type.setVisibility(View.VISIBLE);
			viewHolder.joined_num.setVisibility(View.VISIBLE);
			viewHolder.make_an_appointment.setVisibility(View.GONE);
			viewHolder.group_type.setImageResource(R.drawable.group_over);
			viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num4);
			viewHolder.joined_num.setTextColor(activity.getResources().getColor(R.color.group_over));
			viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
			// 已报满
		} else if ("5".equals(resultList.get(position).getJoinFlag())) {
			viewHolder.limit_num.setVisibility(View.VISIBLE);
			viewHolder.group_type.setVisibility(View.VISIBLE);
			viewHolder.joined_num.setVisibility(View.VISIBLE);
			viewHolder.make_an_appointment.setVisibility(View.GONE);
			viewHolder.group_type.setImageResource(R.drawable.group_full);
			viewHolder.joined_num.setBackgroundResource(R.drawable.group_join_num3);
			viewHolder.joined_num.setTextColor(activity.getResources().getColor(R.color.group_full));
			viewHolder.joined_num.setText(resultList.get(position).getJoinCount());
			// 预约
		} else if ("6".equals(resultList.get(position).getJoinFlag())) {
			viewHolder.limit_num.setVisibility(View.INVISIBLE);
			viewHolder.group_type.setVisibility(View.VISIBLE);
			viewHolder.group_type.setImageResource(R.drawable.group_make_an_appointment);
			viewHolder.joined_num.setVisibility(View.INVISIBLE);
			viewHolder.make_an_appointment.setVisibility(View.VISIBLE);
			// 已预约
		} else if ("7".equals(resultList.get(position).getJoinFlag())) {
			viewHolder.limit_num.setVisibility(View.INVISIBLE);
			viewHolder.group_type.setVisibility(View.VISIBLE);
			viewHolder.group_type.setImageResource(R.drawable.group_make_an_appointment);
			viewHolder.joined_num.setVisibility(View.INVISIBLE);
			viewHolder.make_an_appointment.setVisibility(View.VISIBLE);
		} else {

		}
		Spannable WordtoSpan = new SpannableString("￥"+resultList.get(position).getJoinRmb()); 
		WordtoSpan.setSpan(new AbsoluteSizeSpan(50), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
		viewHolder.rmb.setText(WordtoSpan);
		viewHolder.group_time.setText(resultList.get(position).getTime());

		viewHolder.limit_num.setText("/" + resultList.get(position).getLimitCount());
		// 字体加粗
		TextPaint tp = viewHolder.group_title.getPaint();
		tp.setFakeBoldText(true);
		TextPaint tp2 = viewHolder.feedbackCoin.getPaint();
		tp2.setFakeBoldText(true);
		viewHolder.group_title.setText(resultList.get(position).getTitle());
		viewHolder.feedbackCoin.setText(resultList.get(position).getCoin());
		viewHolder.group_description.setText(resultList.get(position).getSimpleContent());
		// viewHolder.group_description.setText("主办方简介主办方简介主办方简介主办方简介主办方简介主办方简介主办方简介");
		if (!"".equals(resultList.get(position).getLogo())) {
			imageLoader.displayImage(AppConfig.HOST + resultList.get(position).getLogo(), viewHolder.group_img,
					Constants.image_display_options, AppConfig.animateListener);
		} else {
			viewHolder.group_img.setImageResource(R.drawable.ic_error);
		}

		view.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(activity, GroupDetailActivity.class);
				intent.putExtra("messageId", resultList.get(position).getMessageId());
				intent.putExtra("position", position);
				activity.startActivity(intent);
			}
		});
		return view;
	}

	class ViewHolder {

		TextView group_title, group_time, joined_num, limit_num, limit_text, group_content, feedbackCoin,
				group_description, make_an_appointment, rmb;
		LinearLayout myLayout;
		ImageView group_img, isjoin, activity_img, group_time_img, berserk, invitation, end, group_type;
		Button group_join, group_discuss;

	}
}
